package com.ybkj.o2o.zhuanche.controller;

import com.google.common.base.Preconditions;
import com.ybkj.o2o.zhuanche.common.CarNumberHelper;
import com.ybkj.o2o.zhuanche.common.DataGrid;
import com.ybkj.o2o.zhuanche.common.query.Car2Query;
import com.ybkj.o2o.zhuanche.common.query.CarQuery;
import com.ybkj.o2o.zhuanche.common.transport.Error;
import com.ybkj.o2o.zhuanche.common.transport.Result;
import com.ybkj.o2o.zhuanche.controller.v4.ErrorConstantV4;
import com.ybkj.o2o.zhuanche.controller.v4.ResultTransport;
import com.ybkj.o2o.zhuanche.entity.Car;
import com.ybkj.o2o.zhuanche.entity.Insurance;
import com.ybkj.o2o.zhuanche.manager.CarManager;
import com.ybkj.o2o.zhuanche.manager.InsuranceManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/car")
public class CarRes {

    @Autowired
    private CarManager carManager;

    @Autowired
    private InsuranceManager insuranceManager;

    /**
     * 查询车辆.
     *
     * @param companyId  公司id
     * @param carTypeId  车型id
     * @param carNumber  车牌号
     * @param employName 服务人员姓名
     * @param status     状态
     * @param page       分页参数
     * @param rows       分页参数
     * @param appKey     系统key
     */
    @RequestMapping(value = "findByParams", method = RequestMethod.GET)
    public ResponseEntity<Object> findByParams(
            @RequestParam(required = false, value = "companyId") Long companyId,
            @RequestParam(required = false, value = "carTypeId") Long carTypeId,
            @RequestParam(required = false, value = "carNumber") String carNumber,
            @RequestParam(required = false, value = "employName") String employName,
            @RequestParam(required = false, value = "status") Integer status,
            @RequestParam(required = false, value = "page") Integer page,
            @RequestParam(required = false, value = "rows") Integer rows,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            CarQuery carQuery = new CarQuery();
            carQuery.setCompanyId(companyId);
            carQuery.setCarTypeId(carTypeId);
            carQuery.setCarNumber(carNumber);
            carQuery.setEmployName(employName);
            carQuery.setStatus(status);
            carQuery.setAppKey(appKey);
            carQuery.setPage(page);
            carQuery.setRows(rows);


            DataGrid<Car> dataGrid = carManager.findByParams(carQuery);

            Result<DataGrid<Car>> result = new Result<DataGrid<Car>>(0, "成功", dataGrid);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    /**
     * 根据租赁公司id查询车辆.
     */
    @RequestMapping(value = "findByRentalCompanyId", method = RequestMethod.GET)
    public ResultTransport findByRentalCompanyId(
            @RequestParam("rentalCompanyId") Long rentalCompanyId,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(rentalCompanyId != null, "rentalCompanyId不能为空");
        try {
            Long count = carManager.findByRentalCompanyId(rentalCompanyId, appKey);
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(count);

            return instance;
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    /**
     * 添加车辆保险.
     */
    @RequestMapping(value = "insertInsurance", method = RequestMethod.POST)
    public ResponseEntity<Object> insertInsurance(
            @RequestParam("insuranceEndDate") Long insuranceEndDate,
            @RequestParam(required = false, value = "carId") Long carId,
            @RequestParam(required = false, value = "insuranceValidDate") Long insuranceValidDate,
            @RequestParam(required = false, value = "insuranceCode") String insuranceCode,
            @RequestParam(required = false, value = "insuranceType") String insuranceType,
            @RequestParam(required = false, value = "insuranceAmount") Double insuranceAmount,
            @RequestParam(required = false, value = "vehicleLicense") String vehicleLicense,
            @RequestParam(required = false, value = "certificateNo") String certificateNo,
            @RequestParam(required = false, value = "insuranceCompany") String insuranceCompany,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            Insurance insurance = new Insurance();
            insurance.setCarId(carId);
            insurance.setInsuranceCompany(insuranceCompany);
            insurance.setInsyranceEndDate(new Date(insuranceEndDate));
            if (null != insuranceValidDate) {
                insurance.setInsuranceValidDate(new Date(insuranceValidDate));
            }
            insurance.setInsuranceCode(insuranceCode);
            insurance.setInsuranceType(insuranceType);
            insurance.setCertificateNo(certificateNo);
            insurance.setInsuranceAmount(insuranceAmount);
            insurance.setVehicleLicense(vehicleLicense);
            insurance.setAppKey(appKey);
            Car car = carManager.findByCarId(carId, appKey);
            insurance.setVehicleLicense(car.getCarNumber());
            insurance.setCertificateNo(car.getTaxiNo());
            insuranceManager.save(insurance);
            Result<Insurance> result = new Result<Insurance>(0, "成功", null);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }

    @RequestMapping("deletetInsurance")
    @ResponseBody
    public ResultTransport deleteInsurance(
            @RequestParam("id") Long id,
            @RequestParam("appKey") String appKey) {
        try {
            Insurance insurance = insuranceManager.deleteById(id, appKey);
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(insurance);
            return instance;
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping("countCar")
    @ResponseBody
    public ResultTransport countCar(
            @RequestParam("appKey") String appKey) {
        try {
            long total = carManager.count(appKey);
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(total);
            return instance;
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    /**
     * 根据条件分页查询车辆.
     *
     * @param companyName 公司名称
     * @param carType     车型名称
     * @param carNumber   车牌号
     * @param employName  服务人员名称
     * @param status      状态
     * @param page        分页参数
     * @param rows        分页参数
     * @param appKey      区分系统
     */
    @RequestMapping(value = "findByTiaoJian", method = RequestMethod.GET)
    public ResponseEntity<Object> findByTiaoJian(
            @RequestParam(required = false, value = "companyName") String companyName,
            @RequestParam(required = false, value = "carType") String carType,
            @RequestParam(required = false, value = "carNumber") String carNumber,
            @RequestParam(required = false, value = "employName") String employName,
            @RequestParam(required = false, value = "status") String status,
            @RequestParam(required = false, value = "page") Integer page,
            @RequestParam(required = false, value = "rows") Integer rows,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            Car2Query car2Query = new Car2Query();
            car2Query.setCompanyName(companyName);
            car2Query.setCarType(carType);
            car2Query.setCarNumber(carNumber);
            car2Query.setEmployName(employName);
            car2Query.setStatus(status);
            car2Query.setPage(page);
            car2Query.setRows(rows);
            car2Query.setAppKey(appKey);

            DataGrid<Car> dataGrid = carManager.findByTiaojians(car2Query);

            Result<DataGrid<Car>> result = new Result<DataGrid<Car>>(0, "成功", dataGrid);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;

        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    /**
     * 根据车辆类型查询车辆.
     *
     * @param carTypeId 车型id
     */
    @RequestMapping(value = "findByCarTypeId", method = RequestMethod.GET)
    public ResponseEntity<Object> findByCarTypeId(
            @RequestParam("carTypeId") Long carTypeId,
            @RequestParam("appKey") String appKey) {
        ResponseEntity<Object> responseEntity = null;
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(carTypeId != null, "车辆类型不能为空");
        try {
            CarQuery carQuery = new CarQuery();
            carQuery.setAppKey(appKey);
            carQuery.setCarTypeId(carTypeId);

            List<Car> list = carManager.findByCarType(carQuery);

            Result<List<Car>> result = new Result<List<Car>>(0, "成功", list);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;

        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }


    /**
     * 查询单个车辆.
     *
     * @param carId  车辆id
     * @param appKey 系统key
     */
    @RequestMapping(value = "findByCarId", method = RequestMethod.GET)
    public ResponseEntity<Object> findByCarId(
            @RequestParam("carId") Long carId,
            @RequestParam("appKey") String appKey) {
        ResponseEntity<Object> responseEntity = null;
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(carId != null, "carId不能为空");
        try {
            Car car = carManager.findByCarId(carId, appKey);
            Result<Car> result = new Result<Car>(0, "成功", car);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }


    /**
     * 创建车辆.
     *
     * @param companyId       公司id
     * @param companyName     公司名
     * @param carName         车辆名
     * @param carNumber       车牌号
     * @param carTypeId       车型id
     * @param drivingDistance 行驶距离
     * @param buyTime         购买时间
     * @param employId        服务人员id
     * @param employName      服务人员名
     * @param limitLine       限行时间
     * @param insuranceTime   保险时间
     * @param overhaulTime    年检时间
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public ResponseEntity<Object> create(
            @RequestParam("companyId") Long companyId,
            @RequestParam("companyName") String companyName,
            @RequestParam("carName") String carName,
            @RequestParam("carNumber") String carNumber,
            @RequestParam("carTypeId") Long carTypeId,
            @RequestParam("drivingDistance") Double drivingDistance,
            @RequestParam("buyTime") Long buyTime,
            @RequestParam(required = false, value = "employId") Long employId,
            @RequestParam(required = false, value = "employName") String employName,
            @RequestParam("limitLine") Integer limitLine,
            @RequestParam(required = false, value = "insuranceTime") Long insuranceTime,
            @RequestParam(required = false, value = "overhaulTime") Long overhaulTime,
            @RequestParam("appKey") String appKey,
            @RequestParam(required = false, value = "rentalCompanyId") Long rentalCompanyId,
            @RequestParam(required = false, value = "rentalCompanyName") String rentalCompanyName,
            @RequestParam(required = false, value = "plateColor") String plateColor,
            @RequestParam(required = false, value = "brand") String brand,
            @RequestParam(required = false, value = "vehicleType") String vehicleType,
            @RequestParam(required = false, value = "vehicleModel") String vehicleModel,
            @RequestParam(required = false, value = "owner") String owner,
            @RequestParam(required = false, value = "driverProType") String driverProType,
            @RequestParam(required = false, value = "bodyColor") String bodyColor,
            @RequestParam(required = false, value = "engineNo") String engineNo,
            @RequestParam(required = false, value = "vin") String vin,
            @RequestParam(required = false, value = "fuel") String fuel,
            @RequestParam(required = false, value = "permitted") String permitted,
            @RequestParam(required = false, value = "registrationTime") Long registrationTime,
            @RequestParam(required = false, value = "inspectioStatus") Integer inspectioStatus,
            @RequestParam(required = false, value = "rateType") Integer rateType,
            @RequestParam(required = false, value = "vehicleStatus") String vehicleStatus,
            @RequestParam(required = false, value = "displacement") String displacement,
            @RequestParam(required = false, value = "maintenanceStatus") Integer maintenanceStatus,
            @RequestParam(required = false, value = "nextOverhaulTime") Long nextOverhaulTime,
            @RequestParam(required = false, value = "taxiNo") String taxiNo,
            @RequestParam(required = false, value = "taxiAgency") String taxiAgency,
            @RequestParam(required = false, value = "taxiRegion") String taxiRegion,
            @RequestParam(required = false, value = "validTime") Long validTime,
            @RequestParam(required = false, value = "invalidTime") Long invalidTime,
            @RequestParam(required = false, value = "taxiRegisterTime") Long taxiRegisterTime,
            @RequestParam(required = false, value = "deviceSerial") String deviceSerial,
            @RequestParam(required = false, value = "deviceBrand") String deviceBrand,
            @RequestParam(required = false, value = "deviceModel") String deviceModel,
            @RequestParam(required = false, value = "deviceInstallTime") Long deviceInstallTime,
            @RequestParam(required = false, value = "provincial") String provincial,
            @RequestParam(required = false, value = "insurer") String insurer,
            @RequestParam(required = false, value = "insuranceNo") String insuranceNo,
            @RequestParam(required = false, value = "insType") String insType,
            @RequestParam(required = false, value = "insMoney") Double insMoney,
            @RequestParam(required = false, value = "insValidTime") Long insValidTime,
            @RequestParam(required = false, value = "picture") String picture,
            @RequestParam(required = false, value = "platePictureAddress") String platePictureAddress,
            @RequestParam(required = false, value = "gpsIMEI") String gpsIMEI,
            @RequestParam(required = false, value = "flights") String flights,
            @RequestParam(required = false, value = "pictureTransport") String pictureTransport,
            @RequestParam(required = false, value = "address") String address) {
        ResponseEntity<Object> responseEntity = null;
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(companyId != null, "companyId不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(companyName), "companyName不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(carName), "carName不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(carNumber), "carNumber不能为空");
        Preconditions.checkArgument(carTypeId != null, "carTypeId不能为空");
        Preconditions.checkArgument(drivingDistance != null, "drivingDistance不能为空");
        Preconditions.checkArgument(buyTime != null, "buyTime不能为空");
        Preconditions.checkArgument(limitLine != null, "limitLine不能为空");
        Preconditions.checkArgument(CarNumberHelper.isCarNumber(carNumber), "车牌不合法");
        if (employName != null) {
            Preconditions.checkArgument(StringUtils.isNotBlank(employName), "employName不能为空字符");
        }
        try {
            Car car = new Car();
            if (StringUtils.isNotBlank(carNumber) && StringUtils.isNoneBlank(flights)) {
                Car findByNumber = carManager.queryCarByNumberAndFlights(carNumber, flights, appKey);
                if (findByNumber != null) {
                    Error error = new Error(500, "车牌号已存在");
                    responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
                    return responseEntity;
                }
            } else if (StringUtils.isNotBlank(carNumber)) {
                Car findByNumber = carManager.findByNumber(carNumber, appKey);
                if (findByNumber != null) {
                    Error error = new Error(500, "车牌号已存在");
                    responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
                    return responseEntity;
                }
            }
            car.setAppKey(appKey);
            car.setBuyTime(new Date(buyTime));
            car.setCarName(carName);
            car.setCarNumber(carNumber);
            car.setCarTypeId(carTypeId);
            car.setCompanyId(companyId);
            car.setCompanyName(companyName);
            car.setDrivingDistance(drivingDistance);
            car.setEmployId(employId);
            car.setEmployName(employName);
            car.setPictureTransport(pictureTransport);

            if (insuranceTime != null) {
                car.setInsuranceTime(new Date(insuranceTime));
            }
            car.setLimitLine(limitLine);
            car.setOverhaulTime(new Date(overhaulTime));
            car.setStatus(0);

            car.setTotalMileage(drivingDistance);
            car.setPlateColor(plateColor);
            car.setBrand(brand);
            car.setVehicleType(vehicleType);
            car.setVehicleModel(vehicleModel);
            car.setOwner(owner);
            car.setDriverProType(driverProType);
            car.setBodyColor(bodyColor);
            car.setEngineNo(engineNo);
            car.setVin(vin);
            car.setFuel(fuel);
            car.setPermitted(permitted);
            car.setInsurer(insurer);
            car.setInsuranceNo(insuranceNo);
            car.setInsType(insType);
            car.setInsMoney(insMoney);
            car.setPicture(picture);
            car.setPlatePictureAddress(platePictureAddress);
            car.setFlights(flights);
            car.setRentalCompanyId(rentalCompanyId);
            car.setRentalCompanyName(rentalCompanyName);

            if (null != insValidTime) {
                car.setInsValidTime(new Date(insValidTime));
            }
            if (null != registrationTime) {
                car.setRegistrationTime(new Date(registrationTime));
            }
            car.setInspectioStatus(inspectioStatus);
            car.setRateType(rateType);
            car.setVehicleStatus(vehicleStatus);
            car.setDisplacement(displacement);
            car.setMaintenanceStatus(maintenanceStatus);
            if (null != nextOverhaulTime) {
                car.setNextOverhaulTime(new Date(nextOverhaulTime));
            }
            car.setTaxiNo(taxiNo);
            car.setTaxiAgency(taxiAgency);
            car.setTaxiRegion(taxiRegion);
            if (null != validTime) {
                car.setValidTime(new Date(validTime));
            }
            if (null != invalidTime) {
                car.setInvalidTime(new Date(invalidTime));
            }
            if (null != taxiRegisterTime) {
                car.setTaxiRegisterTime(new Date(taxiRegisterTime));
            }
            car.setDeviceSerial(deviceSerial);
            car.setDeviceBrand(deviceBrand);
            car.setDeviceModel(deviceModel);
            car.setAddress(address);
            car.setGpsIMEI(gpsIMEI);
            if (null != deviceInstallTime) {
                car.setDeviceInstallTime(new Date(deviceInstallTime));
            }
            if (StringUtils.isNotBlank(provincial)) {
                String[] split = provincial.split("-");
                car.setProvincial(split[0]);
                car.setCity(split[1]);
                car.setCounty(split[2]);
            }
            Result<Car> result = new Result<Car>(0, "成功", carManager.save(car));
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }


    /**
     * 删除车辆.
     *
     * @param carId 车辆id
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResponseEntity<Object> delete(@RequestParam("carId") Long carId,
                                         @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "appKey不能为空");
        Preconditions.checkArgument(carId != null, "carId不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            Car car = carManager.findByCarId(carId, appKey);
            Preconditions.checkArgument(car != null, "carId不存在");
            carManager.deleteById(car.getId(), car.getAppKey());
            Result<Car> result = new Result<Car>(0, "成功", null);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    /**
     * 修改车辆.
     *
     * @param carId           车辆id
     * @param companyId       公司id
     * @param companyName     公司名称
     * @param carName         车辆名称
     * @param carNumber       车牌号
     * @param status          状态
     * @param carTypeId       车型id
     * @param drivingDistance 行驶距离
     * @param buyTime         购车时间
     * @param employId        服务人员id
     * @param employName      服务人员名称
     * @param limitLine       限行时间
     * @param insuranceTime   保险时间
     * @param overhaulTime    年检时间
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResponseEntity<Object> update(
            @RequestParam("carId") Long carId,
            @RequestParam(value = "companyId", required = false) Long companyId,
            @RequestParam(value = "companyName", required = false) String companyName,
            @RequestParam(value = "carName", required = false) String carName,
            @RequestParam(value = "carNumber", required = false) String carNumber,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "carTypeId", required = false) Long carTypeId,
            @RequestParam(value = "drivingDistance", required = false) Double drivingDistance,
            @RequestParam(value = "buyTime", required = false) Long buyTime,
            @RequestParam(value = "employId", required = false) Long employId,
            @RequestParam(value = "employName", required = false) String employName,
            @RequestParam(value = "limitLine", required = false) Integer limitLine,
            @RequestParam(value = "insuranceTime", required = false) Long insuranceTime,
            @RequestParam(value = "overhaulTime", required = false) Long overhaulTime,
            @RequestParam("appKey") String appKey,
            @RequestParam(required = false, value = "plateColor") String plateColor,
            @RequestParam(required = false, value = "brand") String brand,
            @RequestParam(required = false, value = "vehicleType") String vehicleType,
            @RequestParam(required = false, value = "vehicleModel") String vehicleModel,
            @RequestParam(required = false, value = "owner") String owner,
            @RequestParam(required = false, value = "driverProType") String driverProType,
            @RequestParam(required = false, value = "bodyColor") String bodyColor,
            @RequestParam(required = false, value = "engineNo") String engineNo,
            @RequestParam(required = false, value = "vin") String vin,
            @RequestParam(required = false, value = "fuel") String fuel,
            @RequestParam(required = false, value = "permitted") String permitted,
            @RequestParam(required = false, value = "registrationTime") Long registrationTime,
            @RequestParam(required = false, value = "inspectioStatus") Integer inspectioStatus,
            @RequestParam(required = false, value = "rateType") Integer rateType,
            @RequestParam(required = false, value = "vehicleStatus") String vehicleStatus,
            @RequestParam(required = false, value = "displacement") String displacement,
            @RequestParam(required = false, value = "maintenanceStatus") Integer maintenanceStatus,
            @RequestParam(required = false, value = "nextOverhaulTime") Long nextOverhaulTime,
            @RequestParam(required = false, value = "taxiNo") String taxiNo,
            @RequestParam(required = false, value = "taxiAgency") String taxiAgency,
            @RequestParam(required = false, value = "taxiRegion") String taxiRegion,
            @RequestParam(required = false, value = "validTime") Long validTime,
            @RequestParam(required = false, value = "invalidTime") Long invalidTime,
            @RequestParam(required = false, value = "taxiRegisterTime") Long taxiRegisterTime,
            @RequestParam(required = false, value = "deviceSerial") String deviceSerial,
            @RequestParam(required = false, value = "deviceBrand") String deviceBrand,
            @RequestParam(required = false, value = "deviceModel") String deviceModel,
            @RequestParam(required = false, value = "deviceInstallTime") Long deviceInstallTime,
            @RequestParam(required = false, value = "provincial") String provincial,
            @RequestParam(required = false, value = "insurer") String insurer,
            @RequestParam(required = false, value = "insuranceNo") String insuranceNo,
            @RequestParam(required = false, value = "insType") String insType,
            @RequestParam(required = false, value = "insMoney") Double insMoney,
            @RequestParam(required = false, value = "insValidTime") Long insValidTime,
            @RequestParam(required = false, value = "picture") String picture,
            @RequestParam(required = false, value = "platePictureAddress") String platePictureAddress,
            @RequestParam(required = false, value = "address") String address,
            @RequestParam(required = false, value = "rentalCompanyId") Long rentalCompanyId,
            @RequestParam(required = false, value = "rentalCompanyName") String rentalCompanyName,
            @RequestParam(required = false, value = "flights") String flights,
            @RequestParam(required = false, value = "pictureTransport") String pictureTransport,
            @RequestParam(required = false, value = "gpsIMEI") String gpsIMEI) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(carId != null, "carId不能为空");
        ResponseEntity<Object> responseEntity = null;

        try {
            if (buyTime != null) {
                Preconditions.checkArgument(new Date().after(new Date(buyTime)), "购买日期不能在当前日期之后");
            }
            if (carNumber != null) {
                Preconditions.checkArgument(CarNumberHelper.isCarNumber(carNumber), "车牌不合法");
            }
            Car car = carManager.findByCarId(carId, appKey);
            Preconditions.checkArgument(car != null, "车辆不存在");

            if (StringUtils.isNotBlank(carNumber) && StringUtils.isNoneBlank(flights)) {
                Car findByNumber = carManager.queryCarByNumberAndFlights(carNumber, flights, appKey);
                if (findByNumber != null) {
                    Preconditions.checkArgument(findByNumber.getId().equals(carId), "车牌号已存在");
                }
            } else if (StringUtils.isNotBlank(carNumber)) {
                Car car2 = carManager.findByNumber(carNumber, appKey);
                if (car2 != null) {
                    Preconditions.checkArgument(car2.getId().equals(carId), "车牌号已存在");
                }
            }

            car.setCarName(carName);
            car.setCarNumber(carNumber);
            car.setCarTypeId(carTypeId);
            car.setCompanyId(companyId);
            car.setCompanyName(companyName);
            car.setDrivingDistance(drivingDistance);
            final Long oldEmployId = car.getEmployId();
            if (employId != null) {
                car.setEmployId(employId);
            }
            car.setEmployName(employName);
            car.setLimitLine(limitLine);
            car.setStatus(status);
            if (overhaulTime != null) {
                car.setOverhaulTime(new Date(overhaulTime));
            }
            if (buyTime != null) {
                car.setBuyTime(new Date(buyTime));
            }
            if (insuranceTime != null) {
                car.setInsuranceTime(new Date(insuranceTime));
            }

            car.setTotalMileage(drivingDistance);
            car.setPlateColor(plateColor);
            car.setBrand(brand);
            car.setPictureTransport(pictureTransport);
            car.setVehicleType(vehicleType);
            car.setVehicleModel(vehicleModel);
            car.setOwner(owner);
            car.setDriverProType(driverProType);
            ;
            car.setBodyColor(bodyColor);
            car.setEngineNo(engineNo);
            car.setVin(vin);
            car.setFuel(fuel);
            car.setPermitted(permitted);
            car.setInsurer(insurer);
            car.setInsuranceNo(insuranceNo);
            car.setInsType(insType);
            car.setInsMoney(insMoney);
            car.setPicture(picture);
            car.setPlatePictureAddress(platePictureAddress);
            car.setFlights(flights);
            if (null != insValidTime) {
                car.setInsValidTime(new Date(insValidTime));
            }

            if (null != registrationTime) {
                car.setRegistrationTime(new Date(registrationTime));
            }
            car.setInspectioStatus(inspectioStatus);
            car.setRateType(rateType);
            car.setVehicleStatus(vehicleStatus);
            ;
            car.setDisplacement(displacement);
            car.setMaintenanceStatus(maintenanceStatus);
            if (null != nextOverhaulTime) {
                car.setNextOverhaulTime(new Date(nextOverhaulTime));
            }
            car.setTaxiNo(taxiNo);
            car.setTaxiAgency(taxiAgency);
            car.setTaxiRegion(taxiRegion);
            if (null != validTime) {
                car.setValidTime(new Date(validTime));
            }
            if (null != invalidTime) {
                car.setInvalidTime(new Date(invalidTime));
            }
            if (null != taxiRegisterTime) {
                car.setTaxiRegisterTime(new Date(taxiRegisterTime));
            }
            car.setDeviceSerial(deviceSerial);
            car.setDeviceBrand(deviceBrand);
            car.setDeviceModel(deviceModel);
            car.setAddress(address);
            if (null != deviceInstallTime) {
                car.setDeviceInstallTime(new Date(deviceInstallTime));
            }
            if (StringUtils.isNotBlank(provincial)) {
                String[] split = provincial.split("-");
                car.setProvincial(split[0]);
                car.setCity(split[1]);
                car.setCounty(split[2]);
            }

            car.setRentalCompanyId(rentalCompanyId);
            car.setRentalCompanyName(rentalCompanyName);
            car.setGpsIMEI(gpsIMEI);
            carManager.update(car, oldEmployId);
            Result<Car> result = new Result<Car>(0, "成功", null);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }


    }


    /**
     * 修改车辆状态.
     *
     * @param carId  车辆id
     * @param status 状态
     * @param appKey 系统key
     */
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    public ResponseEntity<Object> updateStatus(
            @RequestParam("carId") Long carId,
            @RequestParam("status") Integer status,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        Preconditions.checkArgument(status != null, "status不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            Car car = carManager.findByCarId(carId, appKey);
            Preconditions.checkArgument(car != null, "carId不存在");
            car.setStatus(status);
            carManager.update(car, null);
            Result<Car> result = new Result<Car>(0, "成功", null);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }

    /**
     * 修改车辆上传状态
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "updateUpStatus", method = RequestMethod.POST)
    public ResponseEntity<Object> updateUpStatus(@RequestParam("id") Long id) {
        Preconditions.checkArgument(id != null, "车辆不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            carManager.updateUpStatus(id);
            Result<Car> result = new Result<Car>(0, "成功", null);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }


    /**
     * 查询保险时间过期车辆.
     */
    @RequestMapping(value = "findInsuranceTimeOverdue", method = RequestMethod.GET)
    public ResponseEntity<Object> findByInsuranceTime(
            @RequestParam(required = false, value = "companyId") Long companyId,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            List<Car> list = carManager.findInsuranceTimeOverdue(companyId, appKey);
            Result<List<Car>> result = new Result<List<Car>>(0, "成功", list);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }


    /**
     * 查询年检过期车辆.
     */
    @RequestMapping(value = "findOverhaulTimeOverdue", method = RequestMethod.GET)
    public ResponseEntity<Object> findByOverhaulTime(
            @RequestParam(required = false, value = "companyId") Long companyId,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            List<Car> list = carManager.findOverhaulTimeOverdue(companyId, appKey);
            Result<List<Car>> result = new Result<List<Car>>(0, "成功", list);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    @RequestMapping(value = "findByIds", method = RequestMethod.GET)
    public ResponseEntity<Object> findByIds(
            @RequestParam(required = false, value = "ids") Long[] ids,
            @RequestParam(required = false, value = "page") Integer page,
            @RequestParam(required = false, value = "rows") Integer rows,
            @RequestParam("appKey") String appKey) {

        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;

        try {
            CarQuery carQuery = new CarQuery();
            carQuery.setIds(ids);
            carQuery.setAppKey(appKey);
            if (page != null) {
                carQuery.setPage(page);
            }
            if (rows != null) {
                carQuery.setRows(rows);
            }

            DataGrid<Car> dataGrid = carManager.findByIds(carQuery);

            Result<DataGrid<Car>> result = new Result<DataGrid<Car>>(0, "成功", dataGrid);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    @RequestMapping(value = "updateByIds", method = RequestMethod.PUT)
    public ResponseEntity<Object> update(
            @RequestParam("ids") Long[] ids,
            @RequestParam("appKey") String appKey) {
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;

        try {
            List<Car> cars = carManager.updateByIds(ids, appKey);
            Result<List<Car>> result = new Result<List<Car>>(0, "成功", cars);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }

    @RequestMapping(value = "finfInsuranceByCarId", method = RequestMethod.GET)
    public ResponseEntity<Object> finfInsuranceByCarId(
            @RequestParam("carId") Long carId,
            @RequestParam("appKey") String appKey,
            @RequestParam(required = false, value = "page") Integer page,
            @RequestParam(required = false, value = "rows") Integer rows) {
        Preconditions.checkArgument(StringUtils.isNotBlank(String.valueOf(carId)), "carId不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
        ResponseEntity<Object> responseEntity = null;
        try {
            DataGrid<Insurance> insurances = insuranceManager.finfInsuranceByCarId(carId, appKey, null, null);
            Result<DataGrid<Insurance>> result = new Result<>(0, "成功", insurances);
            responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
            return responseEntity;
        } catch (Exception ex) {
            ex.printStackTrace();
            Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
            responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }

}
